from pod.std import *
from pod.macros.html.util import tag
import collections

class Join(object):
    def __init__(self, *args):
        self.items = args
        
    def __repr__(self):
        return "".join([str(e) for e in self.items])

class Link(Macro):
    name = "link"
    doctype = "html"
    
    def __init__(self, ctx, ref):
        super(Link, self).__init__()
        self.ctx = ctx
        self.ref = ref
        
    def __call__(self, content):
        anchor = self.ctx.env["label:%s" % self.ref]
        return tag("a", href="#%s" % self.ref)[Macro.flatten(content)]
        #return '<a href="#%s">%s</a>' % (self.ref, Macro.flatten(content))

class SectionMarker(object):
    def __init__(self, name):
        self.name = name
        self.children = []

class Section(Macro):
    name = "section"
    doctype = "html"
    counter = []
    
    def __init__(self, ctx, name):
        super(Section, self).__init__()        
        self.ctx = ctx
        self.name = name
        self.level = sum([1 if isinstance(item, Section) else 0 for item in ctx.scope])
        if self.level > len(Section.counter) - 1:
            Section.counter.append(0)
        elif self.level < len(Section.counter) - 1:
            while self.level < len(Section.counter) - 1:
                Section.counter.pop()
            Section.counter[-1] += 1
        else:
            Section.counter[-1] += 1
        #endif
        self.number = ".".join([str(e) for e in Section.counter])                
        
    def __call__(self, content):        
        if self.ctx.env.get("doc.section%d.numbered" % self.level, None):
            self.name = "%s: %s" % (self.number, self.name)
        self.klass = "section%d" % self.level \
            if ".section%d" % self.level in self.ctx.env["doc.css"] else None        
        
        header = tag("h%d" % (self.level+1), klass=self.klass)[self.name]
        section = tag("div")[header, Macro.flatten(content)]
        return section

class Figure(Macro):
    """
    Usage:
    #figure(label="label", caption="caption") {
        content
    }

    Example:
    #figure(label="circles", caption="Circle diagram") {
        #graphviz(output="test.jpg") {:
            digraph A {
                a->b;
                b->c;
                b->d;
            }
        :}
    }
    """
    name = "figure"
    doctype = "html"
    counter = 0

    def __init__(self, ctx, label=None, caption=None, title=None):
        super(Figure, self).__init__()
        self.label = label
        self.caption = caption
        self.title = title
        ctx.env["label:%s" % self.label] = self
        self.number = Figure.counter
        Figure.counter += 1

    def __call__(self, content):
        anchor = tag("a", name=self.label)
        table = tag("table", width="100%", klass="figure")[
            tag("tr", klass="figure")[
                tag("td", klass="figure")[Macro.flatten(content)]
            ],
            tag("tr", klass="figure")[
                tag("td", klass="figure_caption")[tag("b")["Figure %d:" % self.number], self.caption]
            ]
        ]
        return Join(anchor, table)
        
class SubFigure(Macro):
    name = "subfigure"
    doctype = "html"
    counter = 0
    
    def __init__(self, ctx, label=None):
        super(SubFigure, self).__init__()
        self.label = label
        ctx.env["label:%s" % self.label] = self
        self.number = SubFigure.counter
        SubFigure.counter += 1
        
    def __call_(self, content):
        return Macro.flatten(content)        

class StructuredList(Macro):
    name = "list_tmp"
    doctype = "html"

    def __init__(self, ctx, type="unordered"):
        super(List, self).__init__()        
        if type == "ordered":
            self.head = tag("ol")
        else:
            self.head = tag("ul")

    def __call__(self, content):
        lst = self.head[
            [tag("li")[Macro.flatten(item)] for item in content if isinstance(item, Block)]
        ]
        return lst
        
class Box(Macro):
    name = "box"
    doctype = "html"
    
    def __init__(self, ctx, **kwargs):
        super(Box, self).__init__()
        self.attrs = kwargs
        
    def __call__(self, content):
        box = tag("div", **self.attrs)[
            Macro.flatten(content)
        ]
        return box

class TableOfContents(Macro):
    name = "toc"
    doctype = "html"
    
    def __init__(self, ctx):
        super(TableOfContents, self).__init__()
        self.ctx = ctx
        
    def create(self):
        table = tag("ol")
                    
    def __call__(self, content):
        pass
    